<template>
  <el-table
      ref="multipleTableRef"
      :data="tableData"
      row-key="id"
      style="width: 100%"
      @row-click="handleRowClick"
      highlight-current-row
  >
    <el-table-column label="Date" width="120">
      <template #default="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column property="name" label="Name" width="120" />
    <el-table-column property="address" label="Address" />
  </el-table>
  <div style="margin-top: 20px">
    <el-button @click="clearSelection">Clear selection</el-button>
  </div>
  {{getName}}
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import type { TableInstance } from 'element-plus'

interface User {
  id: number
  date: string
  name: string
  address: string
}

const multipleTableRef = ref<TableInstance>()
const currentRow = ref<User | null>(null)
let getName = ref()
const handleRowClick = (row: User) => {
  if (currentRow.value?.id === row.id) {
    // 如果点击的是当前选中的行，取消选中
    currentRow.value = null
    multipleTableRef.value?.setCurrentRow(null)
    getName.value = ''
  } else {
    // 否则，选中当前行
    currentRow.value = row
    getName.value = row.name
    multipleTableRef.value?.setCurrentRow(row)
  }
}

const clearSelection = () => {
  currentRow.value = null
  multipleTableRef.value?.setCurrentRow(null)
}

const tableData: User[] = [
  {
    id: 1,
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 2,
    date: '2016-05-02',
    name: 'Tom2',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 3,
    date: '2016-05-04',
    name: 'Tom3',
    address: 'No. 189, Grove St, Los Angeles',
  },

]
</script>
<style scoped>
:deep(.el-table__body tr.current-row>td.el-table__cell) {
  background-color: #1890FF;
}
</style>